package com.cskaoyan.shopping.service.impl.yyr;

import com.cskaoyan.mall.commons.exception.yyr.ExceptionProcessorUtils;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ProductCateConverter;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dto.yyr.AllProductCateRequest;
import com.cskaoyan.shopping.dto.yyr.AllProductCateResponse;
import com.cskaoyan.shopping.service.yyr.IProductCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Description：
 * @Author: 杨涌仁
 * @Date: 2022/08/25/0025 14:01
 */
@Slf4j
@Service
public class IProductCateServiceImpl implements IProductCateService {

    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ProductCateConverter productCateConverter;
    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {
        // 创建响应
        AllProductCateResponse response = new AllProductCateResponse();

        try {
            // 进行参数的校验
            request.requestCheck();
            Example example = new Example(ItemCat.class);
            // 排序
            example.setOrderByClause("sort_order " + request.getSort());
            List<ItemCat> itemCats = itemCatMapper.selectByExample(example);
            // 设置状态
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            // 设置数据
            response.setProductCateDtoList(productCateConverter.items2Dto(itemCats));
        } catch (Exception e) {
            // 打印异常日志
            log.error("IProductCateServiceImpl.getAllProductCate occur Exception:" + e);
            // 利用异常处理工具类处理异常
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        // 没有发生异常
        return response;
    }
}
